import 'package:fish_redux/fish_redux.dart';
import 'package:member_center_sdk_example/models/member.dart';

import 'action.dart';
import 'state.dart';

Reducer<MemberCenterState> buildReducer() {
  return asReducer(
    <Object, Reducer<MemberCenterState>>{
      MemberCenterAction.init: _onInitAction,
      MemberCenterAction.noLogin: _onNoLoginAction,
      MemberCenterAction.loadErr: _onLoadErrAction,
      MemberCenterAction.loading: _onLoadingAction,
      MemberCenterAction.updateAvaterUrl: _onUpdateAvaterUrlAction,
      MemberCenterAction.updateNickName: _onUpdateNickNameAction,
      MemberCenterAction.updateEmail: _onUpdateEmailAction,
      MemberCenterAction.updateHasPassword: _onUpdateHasPasswordAction,
    },
  );
}

MemberCenterState _onInitAction(MemberCenterState state, Action action) {
  final Member member = action.payload ?? Member();
  final MemberCenterState newState = fromMember(member);
  newState.isLoading = false;
  newState.loadErr = false;
  newState.isNoLogin = false;
  return newState;
}

MemberCenterState _onLoadErrAction(MemberCenterState state, Action action) {
  final MemberCenterState newState = state.clone();
  newState.loadErr = true;
  newState.isLoading = false;
  newState.isNoLogin = false;
  return newState;
}

MemberCenterState _onNoLoginAction(MemberCenterState state, Action action) {
  final MemberCenterState newState = state.clone();
  newState.isNoLogin = true;
  newState.loadErr = false;
  newState.isLoading = false;
  return newState;
}

MemberCenterState _onLoadingAction(MemberCenterState state, Action action) {
  final MemberCenterState newState = state.clone();
  newState.isLoading = true;
  newState.loadErr = false;
  newState.isNoLogin = false;
  return newState;
}

MemberCenterState _onUpdateAvaterUrlAction(
    MemberCenterState state, Action action) {
  final MemberCenterState newState = state.clone();
  newState.avatarUrl = action.payload;
  return newState;
}
MemberCenterState _onUpdateNickNameAction(
    MemberCenterState state, Action action) {
  final MemberCenterState newState = state.clone();
  newState.nickName = action.payload;
  return newState;
}
MemberCenterState _onUpdateEmailAction(
    MemberCenterState state, Action action) {
  final MemberCenterState newState = state.clone();
  newState.email = action.payload;
  return newState;
}
MemberCenterState _onUpdateHasPasswordAction(
    MemberCenterState state, Action action) {
  final MemberCenterState newState = state.clone();
  newState.hasPassword = action.payload;
  return newState;
}